import { axiosInstance } from 'src/boot/axios'
import CommonConfig from '../../assets/js/config'

import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

const tpmUrl = CommonConfig.tpmUrl + '/v1'
const baseUrlScheme = CommonConfig.schemaUrl + '/v1'
const reportUrl = CommonConfig.reportUrl + '/v1'

/**
 * 获取组织下设备列表
 * schemeId 组织ID
 * schemeLevel 组织层级
 **/
export function getDevices (schemeId, schemeLevel) {
  const reqData = {
    params: {
      schemeLevel
    }
  }
  const newUrl = tpmUrl + `/schemes/${schemeId}/devices`
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res))
  })
}
// devices
// 列举设备
export function getDeviceIds (schemeId, schemeLevel, ifBuildBox = false, availableBox = false) {
  const newUrl = `${tpmUrl}/devices`
  const reqData = {
    params: {
      schemeLevel,
      schemeId,
      ifBuildBox,
      availableBox
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res))
  })
}
// 获取生产组织架构-树形选择器结构
export function getOrganize () {
  const newUrl = baseUrlScheme + '/scheme_tree'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取稼动率
export function getOEEReport (params) {
  const newUrl = `${reportUrl}/oee_pc_reports`
  let reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取稼动率（日）
export function getOEEDailyReport (params) {
  const newUrl = `${reportUrl}/oee_daily_shift_reports`
  let reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取稼动率（周）
export function getOEEWeeklyReport (params) {
  const newUrl = `${reportUrl}/oee_weekly_reports`
  let reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取稼动率（月）
export function getOEEMonthlyReport (params) {
  const newUrl = `${reportUrl}/oee_monthly_reports`
  let reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 导出稼动率
export function exportOEEReport (params) {
  const newUrl = `${reportUrl}/oee_daily_shift_reports/excel:export`
  let reqData = {
    params,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  return axiosInstance.post(newUrl, reqData.params).then((res) => {
    return Promise.resolve(res)
  })
}
